﻿function GetXmlHttpObject()
{
	try { 
		if (window.XMLHttpRequest)// Mozilla, Safari,...
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  return new XMLHttpRequest();
			 if (new XMLHttpRequest().overrideMimeType) {
				 new XMLHttpRequest().overrideMimeType('text/html');
				 }
		  }else if (window.ActiveXObject)
		  {
				 try {
					 return new ActiveXObject("Msxml2.XMLHTTP");
					 } catch (e) {
						try {
							return new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e) {}
					 }
		  // code for IE6, IE5
		   new ActiveXObject("Microsoft.XMLHTTP");
		  }
	} catch(e) {} 
alert("XMLHttpRequest not supported");
return null;
}
function showPage(url) {
	if(url==null){
		url = "welcome.php";
	}
	var str = url.split(".");
	var page = str[0];
	var html = "";
	var loadingHTML ='<div id="waitmain" align=center>Loading... <br><img src = "images/loading/loading.gif" alt="" /></div>';
    var req = GetXmlHttpObject();
    req.onreadystatechange = function () { 
    	if(req.readyState == 1){ document.getElementById('left').innerHTML = loadingHTML; } 
		if(req.readyState == 2){ document.getElementById('left').innerHTML = loadingHTML; } 
		if(req.readyState == 3){ document.getElementById("left").innerHTML = loadingHTML; }
        if(req.readyState==4) {
            var ret=req.responseText; //รับค่ากลับมา
            //alert(ret);
            html = "<a href=\"javascript:showPage('welcome.php');\"";
            if(page == 'welcome'){
            	html += " class='tabon'";
            }
            html += ">อุปกรณ์สำนักงาน</a>";
            html += "<a href=\"javascript:showPage('process.php');\"";
            if(page == 'process'){
            	html += " class=tabon";
            }
            html += "> แจ้งเหตุเสีย</a>";
            html += "<a href=\"javascript:showPage('search.php');\"";
            if(page == 'search'){
            	html += " class=tabon";
            }
            html += ">ค้นหาการแจ้งเสีย</a>";
            html += "<a href=\"javascript:showPage('showdetail.php');\"";
            if(page == 'showdetail'){
            	html += " class=tabon";
            }
            html += ">ประวัติการแจ้ง</a>";
            
            document.getElementById("menutabs").innerHTML = html;
            
            if(page == 'showdetail'|| page == 'search'){
            	document.getElementById("leftshow").innerHTML = ret; //ส่งค่ากลับไปแสดง
            	document.getElementById("left").innerHTML = "";
            	document.getElementById("showright").innerHTML = "";
            }else{
            	document.getElementById("leftshow").innerHTML = "";
            	document.getElementById("left").innerHTML = ret; //ส่งค่ากลับไปแสดง
            	document.getElementById("showright").innerHTML = "<div id=\"right\"><div id='news'><strong class=\"L12\">News?</strong><br /><br />ข่าวอัพเดท  >> </div></div>";
            	
            }
            
        }
    };
    req.open("POST", url, true); //สร้าง connection
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
    req.send(null); //ส่งค่า
}
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function doAddFault(){
	//alert(get_radio());//test value
	var radio = get_radio();
	var loadingHTML = ' <div id="loader" align=center>Loading... <img src = "images/loading/loader.gif" alt="" /></div>';
	var xmlHttp = Inint_AJAX();
	var url = 'addproblem.php';
	var pmeters = "type="+radio+"&location="+encodeURI(document.getElementById("location").value)+"&problem="+encodeURI(document.getElementById("problem").value)+"&filename="+encodeURI(document.getElementById("filename").value);
	//alert(pmeters);//test value
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 0){ document.getElementById('table_form').innerHTML = "Sending Request..."; } 
		if(xmlHttp.readyState == 1){ document.getElementById('table_form').innerHTML = loadingHTML; } 
		if(xmlHttp.readyState == 2){ document.getElementById('table_form').innerHTML = loadingHTML; } 
		if(xmlHttp.readyState == 3){ document.getElementById("table_form").innerHTML = loadingHTML; }
		if(xmlHttp.readyState == 4){
				document.getElementById("loader").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
function get_radio(){
	for(var i=0;i<fault.ftype.length;i++){
		if(fault.ftype[i].checked){
			var rad_val = fault.ftype[i].value;
		}
	}
	return rad_val;
}
function showFaultPage(url) {
	//document.getElementById("formfault").innerHTML = '<div id="waitmain" align=center><br><br><br><br><br><img src = "images/loading/loading.gif" alt="" /></div>';
    var loadingHTML = '<div id="waitmain" align=center>Loading... <img src = "images/loading/loader.gif" alt="" /></div>';
	var req = GetXmlHttpObject();
    req.onreadystatechange = function () { 
    	if (req.readyState==1) {document.getElementById("formfault").innerHTML = loadingHTML;}
    	if (req.readyState==2) {document.getElementById("formfault").innerHTML = loadingHTML;}
    	if (req.readyState==3) {document.getElementById("formfault").innerHTML = loadingHTML;}
    	if (req.readyState==4) {
            var ret=req.responseText; //รับค่ากลับมา
            	document.getElementById("formfault").innerHTML = ret; //ส่งค่ากลับไปแสดง
            }
    };
    req.open("POST", url, true); //สร้าง connection
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
    req.send(null); //ส่งค่า
}
function doHistoryFault(username){
	//alert(username);
	var loadingHTML = ' <div id="loader" align=center>Loading... <img src = "images/loading/loader.gif" alt="" /></div>';
	var xmlHttp = Inint_AJAX();
	var url = 'showdatabase.php';
	var pmeters = "user_see="+username;
	//alert(pmeters);//test value
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 0){ document.getElementById('searchList').innerHTML = "Sending Request..."; } 
		if(xmlHttp.readyState == 1){ document.getElementById('searchList').innerHTML = loadingHTML; } 
		if(xmlHttp.readyState == 2){ document.getElementById('searchList').innerHTML = loadingHTML; } 
		if(xmlHttp.readyState == 3){ document.getElementById("searchList").innerHTML = loadingHTML; }
		if(xmlHttp.readyState == 4){
				document.getElementById("searchList").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
// code หน้าค้นหา ==========================================================================================
	var datestart,datestop;
function getform(form){
		var loadingHTML = ' <div id="loader" align=center>Loading... <img src = "images/loading/loader.gif" alt="" /></div>';
		var xmlHttp = Inint_AJAX();
		var url = 'getform.php';
	if(form == 1){
		document.getElementById("formsearch").innerHTML = "<small><div><strong>วันที่แจ้ง : </strong> &nbsp;<input id='datefault' type='text' name='datefault'> จำนวนรายการต่อหน้า <select name='select_perpage'><option value='10'>10</option><option value='20'>20</option><option value='50'>50</option><option value='100'>100</option></select> <input type='button' onClick=\"javascript:getresult(document.fsearch.datefault.value,1,'',document.fsearch.select_perpage.value);\" value='ค้นหา' class='butsubmit'></div></small>";
		new Epoch('datefault','popup',document.getElementById('datefault'));
		document.getElementById("searchresult").innerHTML = "";
	}else if(form == 2){
		document.getElementById("formsearch").innerHTML = "<small><strong>กรุณาใส่ชื่อผู้แจ้ง :</strong><input name='wordsearch' type='text' id='wordsearch' value='' onClick=\"this.value=''\" > จำนวนรายการต่อหน้า <select name='select_perpage'><option value='10'>10</option><option value='20'>20</option><option value='50'>50</option><option value='100'>100</option></select> <input type='button' onClick=\"javascript:getresult(document.fsearch.wordsearch.value,2,'',document.fsearch.select_perpage.value);\" value='ค้นหา' class='butsubmit'></small>";
		document.getElementById("searchresult").innerHTML = "";
	}else if(form == 4){
		document.getElementById("formsearch").innerHTML = "<small><div><strong>วันเริ่มต้น:</strong> &nbsp;<input id='datestart' type='text' name='datestart'> <strong>วันสุดท้าย </strong>: &nbsp;<input id='datestop' type='text' name='datestop'> จำนวนรายการต่อหน้า <select name='select_perpage'><option value='10'>10</option><option value='20'>20</option><option value='50'>50</option><option value='100'>100</option></select> <input type='button' onClick=\"javascript:getresult(document.fsearch.datestart.value+'-'+document.fsearch.datestop.value,4,'',document.fsearch.select_perpage.value);\" value='ค้นหา' class='butsubmit'></div></small>";
		getcalendar();
		document.getElementById("searchresult").innerHTML = "";
	}else if(form == 3){
		document.getElementById("formsearch").innerHTML = "<small><strong>กรุณาใส่เหตุเสียที่แจ้ง :</strong><input name='wordsearch' type='text' id='wordsearch' value='' onClick=\"this.value=''\" >จำนวนรายการต่อหน้า <select name='select_perpage'><option value='10'>10</option><option value='20'>20</option><option value='50'>50</option><option value='100'>100</option></select> <input type='button' onClick=\"javascript:getresult(document.fsearch.wordsearch.value,3,'',document.fsearch.select_perpage.value);\" value='ค้นหา' class='butsubmit'></small>";
		document.getElementById("searchresult").innerHTML = "";
	}else if(form == 6){
		document.getElementById("formsearch").innerHTML = "<small>กรุณาเลือกสถานะการแจ้ง </small><select name='statusdb' id='statusdb'><option value='1'>ระบบได้รับข้อมูลการแจ้งเสียแล้ว</option><option value='2'>ผู้รับผิดชอบกำลังดำเนินการ</option><option value='3'>อยู่ระหว่างการขออนุมัติ</option><option value='4'>อนุมัติให้ดำเนินการแก้ไขแล้ว</option><option value='5'>ปัญหาได้รับการแก้ไขแล้ว</option><option value='6'>ไม่อนุมัติ</option></select><small>จำนวนรายการต่อหน้า <select name='select_perpage'><option value='10'>10</option><option value='20'>20</option><option value='50'>50</option><option value='100'>100</option></select> &nbsp;<input type='button' onClick=\"javascript:getresult(document.fsearch.statusdb.value,6,'',document.fsearch.select_perpage.value);\" value='ค้นหา' class='butsubmit'>";
		document.getElementById("searchresult").innerHTML = "";
	}else if(form == 5){

		var pmeters = "f="+form;
		//alert(pmeters);//test value
		xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 3){ document.getElementById("formsearch").innerHTML = loadingHTML; }
		if(xmlHttp.readyState == 4){
				document.getElementById("formsearch").innerHTML = xmlHttp.responseText;
				document.getElementById("searchresult").innerHTML = "";
			}
		};
		xmlHttp.open('POST',url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(pmeters);		
		
	}

}
function getcalendar() {  
	//bas_cal = new Epoch('epoch_basic','flat',document.getElementById('basic_container'));
	datestart  = new Epoch('datestart','popup',document.getElementById('datestart'));
	datestop = new Epoch('datestop','popup',document.getElementById('datestop'));
	//ms_cal  = new Epoch('epoch_multi','flat',document.getElementById('multi_container'),true);
}
function getresult(wordSearch,typeSearch,page,per_page){
	//alert(wordSearch);
	//alert(typeSearch);
	if(wordSearch == ""){
		alert('คุณยังไม่ได้ระบุคำค้น กรุณาระบบคำค้นก่อน');
		exit();
	}
	var loading = "<div id='loading'>Loading...</div>";
	var xmlHttp = GetXmlHttpObject();
	var url = 'searchdata.php';
	var pmeters = "wordsearch="+wordSearch+"&typesearch="+typeSearch+"&strpage="+page+"&per_page="+per_page;
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 0){document.getElementById("searchresult").innerHTML = loading;}
		if(xmlHttp.readyState == 1){document.getElementById("searchresult").innerHTML = loading;}
		if(xmlHttp.readyState == 2){document.getElementById("searchresult").innerHTML = loading;}
		if(xmlHttp.readyState == 3){document.getElementById("searchresult").innerHTML = loading;}
		if(xmlHttp.readyState == 4){
			document.getElementById("searchresult").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true); 
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
	}
// จบสคริปหน้าค้นหา ==========================================================================================
// สคริป เช็คกรอกข้อมูล
function chinputFault(){
	for(var i=0;i<fault.ftype.length;i++){
		if(fault.ftype[i].checked){
			var radio = fault.ftype[i].value;
		}
	}
	var location = document.fault.location.value;
	var problem = document.fault.problem.value;
	if(radio == null){
		alert('คุณยังไม่ได้เลือกประเภทเหตุเสีย');
		document.fault.ftype[0].focus();
		return false;
	}
	if(location == ""){
		alert('กรุณาเลือกสถานที่');
		document.fault.location.focus();
		return false;
	}
	if(problem == ""){
		alert('กรุณาระบุเหตุเสีย');
		document.fault.problem.focus();
		return false;
	}else{
		//alert("doAddRault");
		doAddFault();
		//return true;
	}
}
/****************************************************************************/
function GetXmlHttp(){
	var HttPRequest = false;
	if(window.XMLHttpRequest) { // Mozilla, Safari,...
		HttPRequest = new XMLHttpRequest();
		if (HttPRequest.overrideMimeType) {
			HttPRequest.overrideMimeType('text/html');
		}
		return HttPRequest;
	}else if(window.ActiveXObject) { // IE
		try {
			HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
			 return HttPRequest;
	} 	  
	if (!HttPRequest) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
}
function edituser(user) {
		HttPRequest = GetXmlHttp();
		var url = 'admin/user.php';
		var pmeters = "user="+user+"&a=edit&mode=user";
		HttPRequest.onreadystatechange = function(){
			if(HttPRequest.readyState == 3){document.getElementById("sectoruser").innerHTML = "Now is Loading...";}
			if(HttPRequest.readyState == 4){
				document.getElementById("sectoruser").innerHTML = HttPRequest.responseText;
			}		
		};
		HttPRequest.open('POST',url,true);
		HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		HttPRequest.setRequestHeader("Content-length", pmeters.length);
		HttPRequest.setRequestHeader("Connection", "close");
		HttPRequest.send(pmeters);
}
function updateuser(user,mode){
	var pmeters;
			///////////////////////////////// ส่วนของข้อมูลทั่วไป
		if(document.getElementById('fax').value == "" || document.getElementById('fax').value.match(/\s/g)){
			alert('กรุณาใส่เบอร์ แฟกซ์ ');
			document.getElementById('fax').focus();
			return false;
		}
		if(isNaN(document.getElementById('fax').value) && !(document.getElementById('fax').value.match('-'))){
			alert('กรุณาใส่เบอร์ แฟกซ์ ( เฉพาะตัวเลขด้วย ) หรือ - ');
			document.getElementById('fax').focus();
			return false;
		}else if(document.getElementById('tel').value == ""){
			alert('กรุณาใส่เบอร์โทรศัพท์ด้วย ');
			document.getElementById('tel').focus();
			return false;
		}else if(isNaN(document.getElementById('tel').value) && !(document.getElementById('tel').value.match('-'))){
			alert('กรุณากรอก เบอร์โทรศัพท์(เฉพาะตัวเลขด้วย)');
			document.getElementById('tel').focus();
			return false;
		}else{
			var jtel = document.getElementById('tel').value;
			var wspace = /\s/g;
			if(jtel.match(/\s/g)){
			alert('กรุณากรอก เบอร์โทรศัพท์(เฉพาะตัวเลขด้วย)');
			document.getElementById('tel').focus();
			return false;
			}
		}
		// หากมีการคลิกเปลี่ยนรหัส
	if(document.getElementById("changepass").checked == true){
		var newpass1 = document.getElementById("pass1").value;
		var newpass2 = document.getElementById("pass2").value;
		var chp;
		var pass;
		/*ส่วนของรหัสผ่าน*/
		if(document.getElementById('oldpass').value == ""){
			alert('กรุณาใส่รหัสของท่านเพื่อทำการเปลี่ยนรหัสผ่านใหม่');
			document.getElementById('oldpass').focus();
			return false;
		}else if(document.getElementById('pass1').value == "" || document.getElementById('pass1').value.match(/\s/g)){
			alert('กรุณากรอกรหัสผ่านใหม่ และไม่ควรเป็นช่องว่าง ( )');
			document.getElementById('pass1').focus();
			return false;
		}
		else if(document.getElementById('pass2').value == "" || document.getElementById('pass2').value.match(/\s/g)){
			alert('กรุณากรอกรหัสผ่านใหม่อีกครั้ง และไม่ควรเป็นช่องว่าง ( )');
			document.getElementById('pass2').focus();
			return false;
		}
		
		if(newpass1 == newpass2){
			chp = 1;
			pass = newpass1;
		}else{
			alert('คุณใส่รหัสผ่านใหม่ไม่ตรงกันครับ');
			return false;
			chp = 0;
			pass = 'nomatch';
		}
		pmeters = "user="+user+"&a=update&mode="+mode+"&tel="+encodeURI(document.getElementById("tel").value)+"&fax="+encodeURI(document.getElementById("fax").value)+"&oldpass="+encodeURI(document.getElementById("oldpass").value)+"&pass="+pass+"&chp="+chp;
	}else{
		pmeters = "user="+user+"&a=update&mode="+mode+"&tel="+encodeURI(document.getElementById("tel").value)+"&fax="+encodeURI(document.getElementById("fax").value);
	}
	
	var xmlHttp = GetXmlHttp();
	var url = 'admin/user.php'; //encodeURI(document.getElementById("location").value)
	
	//alert(pmeters);//test value
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 0){ document.getElementById('sectoruser').innerHTML = "Sending Request..."; } 
		if(xmlHttp.readyState == 1){ document.getElementById('sectoruser').innerHTML = "Loading..."; } 
		if(xmlHttp.readyState == 2){ document.getElementById('sectoruser').innerHTML = "Loading..."; } 
		if(xmlHttp.readyState == 3){ document.getElementById("sectoruser").innerHTML = "Loading..."; }
		if(xmlHttp.readyState == 4){
				document.getElementById("sectoruser").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
function changePassword(){
	var chbok = document.getElementById("changepass").value;
	
	if(document.getElementById("changepass").checked == true){
	 	document.getElementById("oldpass").disabled = false;
		document.getElementById('pass1').disabled = false;
		document.getElementById('pass2').disabled = false;
	 }else{
		document.getElementById('oldpass').disabled = true;
		document.getElementById('pass1').disabled = true;
		document.getElementById('pass2').disabled = true;
	 }
}
function showDetail(user) {
		HttPRequest = GetXmlHttp();
		var url = 'admin/user.php';
		var pmeters = "user="+user+"&a=showdetail&mode=user";
		HttPRequest.onreadystatechange = function(){
			if(HttPRequest.readyState == 3){document.getElementById("sectoruser").innerHTML = "Now is Loading...";}
			if(HttPRequest.readyState == 4){
				document.getElementById("sectoruser").innerHTML = HttPRequest.responseText;
			}		
		};
		HttPRequest.open('POST',url,true);
		HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		HttPRequest.setRequestHeader("Content-length", pmeters.length);
		HttPRequest.setRequestHeader("Connection", "close");
		HttPRequest.send(pmeters);
}

function HideContent(d) {
	document.getElementById(d).style.display = "none";
	}
function ShowContent(d) {
	document.getElementById(d).style.display = "block";
	}
function ReverseDisplay(d) {
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}